/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import model.Endereco;
import util.Conexao;

/**
 *
 * @author Ewerton
 */
public class EnderecoDAO extends GenericDao<Endereco>{

    Conexao c = new Conexao();
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("Controle_de_EstoquePU");
    EntityManager manager = factory.createEntityManager();

    public void salvar(Endereco endereco){
        gravar(endereco);
    }

    public void alterar(Endereco endereco){
        atualizar(endereco);
    }

    public void excluir(Endereco endereco){
        Endereco e = listar(endereco);
        remover(e);
    }

    public Endereco listar(Endereco endereco){
        return getEntityManager().find(Endereco.class, endereco.getId());
    }

    public int PegaUltimoRegistro() {
        Query query = manager.createQuery("SELECT max(e.id) FROM Endereco e");
        Integer codigo = (Integer)query.getSingleResult();
        if(codigo == null){
            return 1;
        }else{
            return codigo + 1;
        }
    }

}
